﻿Imports ccapps.persistent.OCCViewDelete
Public Class OccViewDelete
    Inherits [lib].BasePage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.PageTitle = "OCC Silme Geçmis Görüntüleme "
        If Session("user") = "" Or Session("user") Is Nothing Then
            ' Response.Redirect("../../login.aspx")
            Session("message") = "!!Session Timeout!!,Tekrar Giriş Yapmalısınız"
            Response.Clear()
            Response.Status = "302 Object Moved"
            Response.RedirectLocation = "CX_Exception.aspx"
            Response.End()
        End If

    End Sub

    Protected Sub btnCheckCustcode_Click(sender As Object, e As EventArgs) Handles btnCheckCustcode.Click
        Dim currentPageIndex = Me.DG_OCCTable.CurrentPageIndex
        OCCInfotable.Visible = True
        OCCDataGridtable.Visible = True
        CustcodeSearch()
        OCCDeleteLevel.Text = "Customer"
        lblCOID.Text = "N/A"
    End Sub

    Private Sub CustcodeSearch()
        Try
            DG_OCCTable.Visible = False
            lblAbone.Text = "N/A"
            lblCustomerID.Text = "N/A"
            lblCOID.Text = "N/A"
            Dim pin_Custcode As String = TB_Custcode.Text
            Dim dt As DataTable = GetCustInfoFromCustcode(pin_Custcode)
            If dt.Rows.Count > 0 Then
                For Each dr2 As DataRow In dt.Rows
                    lblAbone.Text = dr2("ccfname").ToString() + " " + dr2("cclname").ToString() + "," + dr2("CCCITY").ToString()
                    lblCustomerID.Text = dr2("CUSTOMER_ID").ToString()
                Next
            Else
                ErrorMessage.Text = "Girmiş olduğunuz Custcode'a ait veri bulunamamıştır."
            End If
        Catch ex As Exception
            ErrorMessage.Text = ex.Message

        End Try
      
    End Sub

    Protected Sub btnCheckMsisdn_Click(sender As Object, e As EventArgs) Handles btnCheckMsisdn.Click
        OCCInfotable.Visible = True
        OCCDataGridtable.Visible = True
        MSISDNSearch()
        OCCDeleteLevel.Text = "Contract"
    End Sub

    Private Sub MSISDNSearch()
        Try
            DG_OCCTable.Visible = False
            lblAbone.Text = "N/A"
            lblCustomerID.Text = "N/A"
            lblCOID.Text = "N/A"
            Dim dt As DataTable = GetCustInfoFromMSISDN(TB_MSISDN.Text.Trim())
            If dt.Rows.Count > 0 Then
                For Each dr2 As DataRow In dt.Rows
                    lblAbone.Text = dr2("ccfname").ToString() + " " + dr2("cclname").ToString() + "," + dr2("CCCITY").ToString()
                    lblCustomerID.Text = dr2("CUSTOMER_ID").ToString().Trim()
                    lblCOID.Text = dr2("CO_ID").ToString()
                Next
            Else
                ErrorMessage.Text = "Girmiş olduğunuz MSISDN'e ait veri bulunamamıştır."
            End If
        Catch ex As Exception
            ErrorMessage.Text = ex.Message()
        End Try

       
    End Sub

    Private Sub GetDisplayOCCHistoryWithCustcode(ByVal pageIndex As Integer)
        If lblCustomerID.Text() <> Nothing Then
            Dim pin_customer_id As Integer = Convert.ToInt32(lblCustomerID.Text)
            Dim dt As DataTable = DisplayOCCHistoryWithCustomerID(pin_customer_id)
            Me.DG_OCCTable.CurrentPageIndex = pageIndex
            DG_OCCTable.AllowPaging = True
            DG_OCCTable.DataSource = dt
            DG_OCCTable.DataBind()
            ErrorMessage.Text = ""
        Else
            ErrorMessage.Text = "Bu custcode ait kayıt bulunanmamıstır."
        End If
    End Sub

    Private Sub GetDisplayOCCHistoryWithMSISDN(ByVal pageIndex As Integer)
        If lblCustomerID.Text() <> "N/A" Then
            Dim pin_customer_id As Integer = Convert.ToInt32(lblCustomerID.Text)
            Dim pin_CO_ID As Integer = Convert.ToInt32(lblCOID.Text)
            Me.DG_OCCTable.CurrentPageIndex = pageIndex
            DG_OCCTable.AllowPaging = True
            Dim dt As DataTable = DisplayOCCHistoryWithCOID(pin_customer_id, pin_CO_ID)
            DG_OCCTable.DataSource = dt
            DG_OCCTable.DataBind()
            ErrorMessage.Text = ""
        Else
            ErrorMessage.Text = "Bu numaraya ait kayıt bulunanmamıstır."
        End If
    End Sub


    Protected Sub DG_OCCTable_Delete(source As Object, e As DataGridCommandEventArgs) Handles DG_OCCTable.ItemCommand
        Dim confirmValue As String = Request.Form("confirm_value")
        If confirmValue = "Yes" Then
            Dim currentPageIndex = Me.DG_OCCTable.CurrentPageIndex
            Try
                If e.CommandName = "view" Then
                    If (OCCDeleteLevel.Text = "Customer") Then
                        Dim dt As DataTable = DeleteOCCFromCustomer(lblCustomerID.Text, e.Item.Cells(2).Text, Session("user"))
                        If dt.Rows.Count > 0 Then
                            For Each dr2 As DataRow In dt.Rows
                                If (dr2("result").ToString() = "OK") Then
                                    ErrorMessage.Text = "Güncelleme İşlemi Başarıyla Gerçekleşti"
                                Else
                                    ErrorMessage.Text = "Güncelleme İşleminde Hata Oluşmuştur "
                                End If
                            Next
                            GetDisplayOCCHistoryWithCustcode(currentPageIndex)
                        End If
                    End If

                    If (OCCDeleteLevel.Text = "Contract") Then
                        Dim dt As DataTable = DeleteOCCFromContract(lblCustomerID.Text, lblCOID.Text, e.Item.Cells(2).Text, Session("user"))
                        If dt.Rows.Count > 0 Then
                            For Each dr2 As DataRow In dt.Rows
                                If (dr2("result").ToString() = "OK") Then
                                    ErrorMessage.Text = "Güncelleme İşlemi Başarıyla Gerçekleşti"
                                Else
                                    ErrorMessage.Text = "Güncelleme İşleminde Hata Oluşmuştur "
                                End If
                            Next
                        End If
                        GetDisplayOCCHistoryWithMSISDN(currentPageIndex)
                    End If
                End If

            Catch ex As Exception
                Session("message") = ex.Message
                Response.Clear()
                Response.Status = "302 Object Moved"
                Response.RedirectLocation = "CX_Exception.aspx"
                Response.End()
            End Try
        End If
        
    End Sub

    
    Protected Sub Change(source As Object, e As DataGridPageChangedEventArgs) Handles DG_OCCTable.PageIndexChanged
        If OCCDeleteLevel.Text = "Customer" Then
            Me.GetDisplayOCCHistoryWithCustcode(e.NewPageIndex)
        End If
        If OCCDeleteLevel.Text = "Contract" Then
            GetDisplayOCCHistoryWithMSISDN(e.NewPageIndex)
        End If
    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            DG_OCCTable.Visible = True
            DG_OCCTable.DataSource = Nothing
            DG_OCCTable.DataBind()
            Dim currentPageIndex = Me.DG_OCCTable.CurrentPageIndex
            If OCCDeleteLevel.Text = "Customer" Then
                GetDisplayOCCHistoryWithCustcode(currentPageIndex)
            End If
            If OCCDeleteLevel.Text = "Contract" Then
                GetDisplayOCCHistoryWithMSISDN(currentPageIndex)
            End If
        Catch ex As Exception
            ErrorMessage.Text = ex.Message
        End Try
        
    End Sub
End Class